﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VNet.VisiSuite.VisiAdmin.Common.Helpers;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class AreaModel : ComparableBaseModel<AreaModel>, IDataErrorInfo
    {
        public AreaModel()
        {
            Cells = new ObservableCollection<CellModel>();
        }

        private int _areaId;
        public int AreaId
        {
            get { return _areaId; }
            set
            {
                _areaId = value;
                OnPropertyChanged("AreaId");
            }
        }

        public bool IsSaveEnabled
        {
            get
            {
                return IsChange && !string.IsNullOrEmpty(_areaName) && !string.IsNullOrEmpty(_description);
            }
        }

        private int _plantId;
        public int PlantId
        {
            get { return _plantId; }
            set
            {
                _plantId = value;
                OnPropertyChanged("PlantId");
            }
        }

        private string _areaName;
        [Category("Properties")]
        [DisplayName("Area Name")]
        [Description("Area Name.")]
        [DefaultValue("")]
        public string AreaName
        {
            get { return _areaName; }
            set
            {
                _areaName = value;
                OnPropertyChanged("AreaName", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _description;
        [Category("Properties")]
        [DisplayName("Description")]
        [Description("Area description.")]
        [DefaultValue("")]
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private bool _enabled;
        [Category("Properties")]
        [DisplayName("Enabled")]
        [Description("Enabled / Disabled this area.")]
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                _enabled = value;
                OnPropertyChanged("Enabled", true);
            }
        }

        private string _createdBy;
        [Category("Update History")]
        [DisplayName("Created By")]
        [Description("User who created the area in VisiAdmin.")]
        public string CreatedBy
        {
            get { return _createdBy; }
            set
            {
                _createdBy = value;
                OnPropertyChanged("CreatedBy");
            }
        }

        private string _createdDate;
        [Category("Update History")]
        [DisplayName("Created On")]
        [Description("Date when the area was created in VisiAdmin.")]
        public string CreatedDate
        {
            get { return _createdDate; }
            set
            {
                _createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        private string _lastUpdatedBy;
        [Category("Update History")]
        [DisplayName("Updated By")]
        [Description("User who last edited the area in VisiAdmin.")]
        public string LastUpdatedBy
        {
            get { return _lastUpdatedBy; }
            set
            {
                _lastUpdatedBy = value;
                OnPropertyChanged("LastUpdatedBy");
            }
        }

        

        private string _lastUpdatedDate;
        [Category("Update History")]
        [DisplayName("Updated On")]
        [Description("Date when the area was last edited in VisiAdmin.")]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        private ObservableCollection<CellModel> _cells;
        public ObservableCollection<CellModel> Cells
        {
            get { return _cells; }
            set
            {
                _cells = value;
                OnPropertyChanged("Cells");
            }

        }

        private bool _isEdited;
        public bool IsEdited
        {
            get { return _isEdited; }
            set
            {
                _isEdited = value;
                OnPropertyChanged("IsEdited");
                OnPropertyChanged("IsItalic");
            }
        }

        private bool _isDeleted;
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set
            {
                _isDeleted = value;
                OnPropertyChanged("IsDeleted", true);
                OnPropertyChanged("IsItalic");
            }
        }

        public bool IsItalic
        {
            get { return IsDeleted || IsEdited; }
        }

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                return string.Empty;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(AreaModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(AreaName, other.AreaName)
                && string.Equals(Description, other.Description)
                && IsDeleted == other.IsDeleted
                && Enabled == other.Enabled
                && Cells.IsCollectionEqual(other.Cells);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((AreaModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (AreaName ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Description ?? string.Empty).GetHashCode();
                result = (result * 397) ^ IsDeleted.GetHashCode();
                result = (result * 397) ^ Enabled.GetHashCode();
                result = (result * 397) ^ (Cells ?? new ObservableCollection<CellModel>()).GetHashCode();
                return result;
            }
        }
        #endregion
    }
}
